﻿using ScFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public sealed class UIEditManager : LogicModuleBase<UserManager>
{
     UIEditCache editCache=null;
    int lastID;
    bool lastValue;
    int lastDragID;
    bool lastDragValue;
    void sendEditCacheMsg()
    {
        lastID = editCache.CurretSelectedObjectId;
        lastValue = editCache.IsObjectValueShowing;
        Dispatch(CombineUIEvent.ShowObjectValues, editCache.IsObjectValueShowing ? (int)editCache.CurretSelectedObjectId : (int)-1,false);
    }
    void sendDragMsg()
    {
        lastDragID = editCache.CurrentDragObjectId;
        lastDragValue = editCache.IsDraggingObject;
        Dispatch(CombineUIEvent.DragObject, editCache.IsDraggingObject ? (int)editCache.CurrentDragObjectId : (int)-1, false);
    }
    public override void OnInit(object args)
    {

        base.OnInit(args);

        editCache = GameObject.FindObjectOfType<UIEditCache>();

        sendEditCacheMsg();
        sendDragMsg();


        if (SysModule.IsHost == false)
        {
            return;
        }
        var cache = GameObject.FindObjectOfType<UIEditCache>();
        foreach(var x in cache.ObjectList)
        {
            Dispatch(EntityDataReq.Create,new EntityCreateWithActionArgs (
                new DataCreateArgs
                {
                    ID = EntityUtilities.GetIdByName(x.name),
                    actions = (EntityData d) =>
                    {
                        d.GetComponentData<SComBody>().SetValue("UserID", SysModule.UserID);
                        d.GetComponentData<SComBody>().SetValue("PresentPos", 2);
                    }
                })
               ,false);
        }
    }
    [RegisterEvent(CombineUIEvent.ShowObjectValues)]
    void Test(int id)
    {
        Debug.Log("Show:" + id);
    }
    public override void FixedUpdate()
    {
        base.FixedUpdate();
        if (lastValue != editCache.IsObjectValueShowing||lastID!=editCache.CurretSelectedObjectId)
        {
      //      lastID = editCache.CurretSelectedObjectId;
       //     lastValue = editCache.IsObjectValueShowing;
            sendEditCacheMsg();
        }
        if (lastDragValue != editCache.IsDraggingObject || lastDragID != editCache.CurrentDragObjectId)
        {
            sendDragMsg();
        }

    }
}
